﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusTransform : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { "RotateAngle", "ScaleValue" }; } }
        public override string[] TestStates { get { return new string[] { "RotateAngle", "ScaleValue" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }

        public double ScaleValue { get { return _scaleValue; } set { _scaleValue = value; Load(); } }
        public double RotateAngle { get { return _rotateAngle; } set { _rotateAngle = value; Load(); } }

        private FinPlusWindow _window;
        private FinPlusApplication _application;
        private bool _hasInitialised;
        private double _rotateAngle, _scaleValue;
        private double _height, _width;

        //construct
        public FinPlusTransform()
        {
            InitializeComponent();
        }
    
        //common control interface 
        public override void Dock(object control, string dock) { }

        public override void Initialise()
        {
            try
            {
                if (_hasInitialised) return;

                _window = ControlSearch.FindVisualParent<FinPlusWindow>(this);
                _application = ControlSearch.FindChild<FinPlusApplication>(_window);

                if (_window == null || _application == null) return;

                _height = _window.Height;
                _width = _window.Width;
                IsValid = _hasInitialised = true;
            }
            catch
            {
                IsValid = false;
            }
        }

        public override void CommandRouter(IFinPlusFunction func) 
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.Scale: Scale(); break;
                    case Commands.Rotate: Rotate(); break;
                    case Commands.Load: Load(); break;
                    case Commands.Clear: Clear(); break;
                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        public override void Dispose() { }

        //private
        private void Scale()
        {
            _scaleValue = scaleSlider.Value;
            Load();
        }

        private void Rotate()
        {
            var height = _window.Height;
            var width = _window.Width;
            _rotateAngle = _rotateAngle + 90 % 360;
            Load();
            _window.Width = height;
            _window.Height = width;
        }

        private void Load()
        {
            Initialise();

            if (!_hasInitialised) return;

            var group = new TransformGroup();
            group.Children.Add(new RotateTransform(_rotateAngle));
            group.Children.Add(new ScaleTransform(_scaleValue, _scaleValue));
            _application.LayoutTransform = group;
            _application.UpdateLayout();
        }

        private void Clear()
        {
            _scaleValue = 1;
            _rotateAngle = 0;
            _window.Height =  _height;
            _window.Width = _width;
            _application.LayoutTransform = null;
            _application.UpdateLayout();
        }

        //cmds
        private new enum Commands { Scale, Rotate, Load, Clear }

        private void Slider_Changed(object sender, RoutedPropertyChangedEventArgs<double> a) { CommandRouter(new Function(Commands.Scale.ToString(), Id, User)); }
        private void Rotate_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.Rotate.ToString(), Id, User)); }
        private void Control_Loaded(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.Load.ToString(), Id, User)); }
        private void Clear_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.Clear.ToString(), Id, User)); }
    }
}














































































































































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus